package cn.tinlan.shopcoin.app.gooddetail.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.donkingliang.labels.LabelsView;

import java.util.ArrayList;
import java.util.List;

import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.app.gooddetail.model.GoodDetailModel;
import cn.tinlan.shopcoin.widget.category.Constants;

/**
 * Created by Mortimer on 2018/11/26. 15:24
 * 佛祖🙏，永无bug。
 */
public class GoodSizePopupwindow extends PopupWindow implements View.OnClickListener{
    private final LabelsView labelsView;
    private final LabelsView labelsView2;
    private final View minusView;
    private final View addView;
    private ImageView popGoodIMG;
//    private Context mContext;
    private View view;
    private TextView goodNumTV;
    private TextView OKBtn;
//    private TextView rightBtn;
    private TextView goodPriceTV;
    private TextView goodStockTV;

    public static final int backcolor       = 0x66000000;

    private List<String> label = new ArrayList<>();
    private List<String> label2 = new ArrayList<>();

    private String size;
    private String size2;

    int num;


    public GoodSizePopupwindow(Context mContext, GoodDetailModel model, View.OnClickListener itemsOnClick) {
        this.view = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_goods_rule, null);
        minusView = view.findViewById(R.id.good_jian_num);
        addView = view.findViewById(R.id.good_add_num);
        labelsView = (LabelsView) view.findViewById(R.id.labels);
        OKBtn = (TextView)view.findViewById(R.id.good_popwin_btn);
//        rightBtn = (TextView)view.findViewById(R.id.good_popwin_rightbtn);
        goodNumTV = (TextView)view.findViewById(R.id.pop_good_num);
        goodPriceTV = (TextView)view.findViewById(R.id.pop_good_price);
        goodStockTV = (TextView)view.findViewById(R.id.pop_good_stock);


        popGoodIMG = (ImageView)view.findViewById(R.id.pop_good_img);
        Glide.with(mContext).load(Constants.BathURL+model.getThumb()).into(popGoodIMG);

        goodPriceTV.setText("￥"+model.getPrice());
        goodStockTV.setText("库存"+model.getStock()+"件");

//        // 设置按钮监听
        minusView.setOnClickListener(this);
        addView.setOnClickListener(this);
        OKBtn.setOnClickListener(itemsOnClick);
//        rightBtn.setOnClickListener(itemsOnClick);


//        ArrayList<String> label = new ArrayList<>();
//        label.add("白色");
//        label.add("黑色");
//        label.add("图片色");
//        label.add("藕粉色");
//        label.add("卡其色");
//        label.add("浅蓝色(比图片较深，介意勿拍)");
//        label.add("粉色");
        labelsView.setLabels(label); //直接设置一个字符串数组就可以了。

//        ======================================================
        labelsView2 = (LabelsView) view.findViewById(R.id.labels2);
//        ArrayList<String> label2 = new ArrayList<>();
//        label2.add("34");
//        label2.add("35");
//        label2.add("36(尺码偏小,请拍小一号)");
//        label2.add("37");
//        label2.add("38");
//        label2.add("39");
//        label2.add("40");
//        label2.add("41");
//        label2.add("42");
        labelsView2.setLabels(label2); //直接设置一个字符串数组就可以了。

        bindClickEvent();

        // 设置外部可点击
        this.setOutsideTouchable(true);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        this.view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = view.findViewById(R.id.pop_layout).getTop();

                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });


        /* 设置弹出窗口特征 */
        // 设置视图
        this.setContentView(this.view);
        // 设置弹出窗体的宽和高
        this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);

        // 设置弹出窗体可点击
        this.setFocusable(true);
        this.setBackgroundDrawable(new ColorDrawable(backcolor));

        // 设置弹出窗体显示时的动画，从底部向上弹出
        this.setAnimationStyle(R.anim.take_photo_anim);

    }

    private void bindClickEvent() {
        labelsView.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
            @Override
            public void onLabelClick(TextView label, Object data, int position) {
                size = (String) data;
                if (onGoodSizeChooseListener != null) {
                    onGoodSizeChooseListener.OnGoodSizeChooseResult(size,size2);
                }
            }
        });
        labelsView2.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
            @Override
            public void onLabelClick(TextView label, Object data, int position) {
                size2 = (String) data;
                if (onGoodSizeChooseListener != null) {
                    onGoodSizeChooseListener.OnGoodSizeChooseResult(size,size2);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.good_jian_num:

                String tempnum = goodNumTV.getText().toString();
                num = Integer.parseInt(tempnum);
                if (num <= 1) return;
                num--;
                goodNumTV.setText(num+"");
                if (onGoodSizeChooseListener != null) {
                    onGoodSizeChooseListener.OnGoodNumResult(num);
                }
                break;

            case R.id.good_add_num:
                String num2 = goodNumTV.getText().toString();
                num = Integer.parseInt(num2);
                num++;
                goodNumTV.setText(num+"");
                if (onGoodSizeChooseListener != null) {
                    onGoodSizeChooseListener.OnGoodNumResult(num);
                }
                break;
        }
    }


    public interface OnGoodSizeChooseListener{
        public void OnGoodSizeChooseResult(String size, String size2);
        public void OnGoodNumResult(int num);
    }

    private OnGoodSizeChooseListener onGoodSizeChooseListener;

    public void setOnGoodSizeChooseListener(OnGoodSizeChooseListener onGoodSizeChooseListener) {
        this.onGoodSizeChooseListener = onGoodSizeChooseListener;
    }
}
